package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/*
* 员工管理Controller
* */

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

//    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                       ) {

        // 防止用户传的值小于1
        if (page < 1) {
            page = 1;
        }

        log.info("查询分页，参数：{},{}", page, pageSize);

        // 调用service分页查询
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);

        return Result.success(pageBean);
    }



    /*
    * 批量删除员工
    * */
    @DeleteMapping("/{ids}")
    public Result batchDeleteEmp(@PathVariable List<Integer> ids) {

        empService.batchDeleteEmp(ids);

        return Result.success("批量删除成功");
    }


    /*
    * 新增员工
    * */
    @PostMapping
    public Result add(@RequestBody Emp emp) {

        empService.save(emp);

        return Result.success("添加成功");
    }
}
